# @Time    : 2020/4/7 21:47
# @Author  : wumao
# @Email   : kanhebei@dingtalk.com

from ._base import BaseModel
from ..extends.flask_sqlalchemy import db
from flask import url_for


association_table = db.Table(
    'tag_article_association',
    db.Column('tag_id', db.ForeignKey('tag.id')),
    db.Column('article_id', db.ForeignKey('article.id')),
    db.UniqueConstraint('tag_id', 'article_id', name='tag_union_article'),
)


class Tag(BaseModel):
    tag_name = db.Column(db.String(64), nullable=False, unique=True, comment='Tag名称')
    tag_title = db.Column(db.String(64), nullable=False, default='', comment='网页标题')
    tag_keyword = db.Column(db.String(64), nullable=False, default='', comment='网页关键词')
    tag_description = db.Column(db.String(64), nullable=False, default='', comment='网页描述')
    num = db.Column(db.Integer, nullable=False, default=0, comment='文章数')
    good = db.Column(db.SmallInteger, index=True, nullable=False, default=0, comment='推荐')

    article = db.relationship('Article', secondary=association_table, back_populates='tag')

    @property
    def url(self):
        return url_for('home.tag_select', tag_id=self.id)

    @property
    def title(self):
        return self.tag_title if self.tag_title else self.tag_name

    @property
    def keyword(self):
        return self.tag_keyword

    @property
    def description(self):
        return self.tag_description

    @property
    def name(self):
        return self.tag_name
